package com.testdemo.web.controller; 
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; 
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.testdemo.web.service.QuestionService;
import com.testdemo.web.vo.Question;
import com.testdemo.web.vo.QuestionTypeCount;
import com.testdemo.web.vo.User; 
 
@Controller
@RequestMapping("/question")
public class QuestionController { 
  
	@Resource
	private QuestionService questionService;
 
	@RequestMapping(value = "/list", 
			method = RequestMethod.GET) 
	public ModelAndView listMyQuestion
	(Integer pagenum, HttpSession session) { 	 
	 
		User u = (User)session.getAttribute("currUser"); 
		if (u == null)
		{
			ModelAndView mav = new ModelAndView("loginUser");
			return mav;
		} 
		if(pagenum == null)
			pagenum = 1;
		ModelAndView mav = new ModelAndView("myQuestionList");
		PageHelper.startPage(pagenum, 3);
		List<Question> list = questionService.listQuestion(u.getUserId()); 
		mav.addObject("list", list);
		PageInfo<Question> pageInfo = new PageInfo<Question>(list);
		mav.addObject("page", pageInfo); 
	 
		return mav;
	}
	
	@RequestMapping(value = "/count",
			method = RequestMethod.GET) 
	public ModelAndView countMyQuestion(
			HttpSession session) { 	 
		
		User u = (User)session.getAttribute("currUser"); 
		if (u == null)
		{
			ModelAndView mav = new ModelAndView("loginUser");
			return mav;
		}  
		ModelAndView mav = new ModelAndView("myQuestionCount");
		PageHelper.startPage(1, 10);
		List<QuestionTypeCount> list = questionService.countQuestion(u.getUserId());
		mav.addObject("list", list);
		PageInfo<QuestionTypeCount> pageInfo = new PageInfo<QuestionTypeCount>(list);
		mav.addObject("page", pageInfo); 
		return mav;
	}
}